/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmConfiguraciones.java
 *
 * Created on 10-oct-2011, 17:03:29
 */
package apiformularios.form;

import api.dom.Cambio;
import api.gest.GestCambio;
import api.gest.GestFactura;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import java.sql.*;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.view.JRDesignViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.*;
import javax.swing.*;
import net.sf.jasperreports.view.JasperViewer;
import server.dom.BaseDeDatos;

/**
 *
 * @author Luis
 */
public class FrmConfiguraciones extends javax.swing.JInternalFrame {
    
    Connection con = null;

    /** Creates new form FrmConfiguraciones */
    public FrmConfiguraciones() {
        initComponents();
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        this.txtValorDolar.setText(String.valueOf(GestCambio.getInstance().devolverUnicoCambio().getDolares()));
        this.txtValorGasOil.setText(String.valueOf(GestCambio.getInstance().devolverUnicoCambio().getGasOil()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel15 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtValorDolar = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtNuevoValorDolar = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 14), new java.awt.Dimension(0, 14), new java.awt.Dimension(32767, 14));
        jPanel4 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        txtimp = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtValorGasOil = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtNuevoValorGasOil = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setNormalBounds(new java.awt.Rectangle(0, 0, 650, 570));
        setPreferredSize(new java.awt.Dimension(650, 570));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(153, 180, 209));
        jPanel1.setPreferredSize(new java.awt.Dimension(580, 71));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel15.setBackground(new java.awt.Color(51, 255, 51));
        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel15.setText("Configuracion");
        jLabel15.setToolTipText("");
        jPanel1.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 1, -1, 70));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 650, 70));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cambio", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jPanel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("Dolar :");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));

        txtValorDolar.setEditable(false);
        txtValorDolar.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel2.add(txtValorDolar, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 80, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Actual");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, -1, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setText("Nuevo");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 20, -1, -1));

        txtNuevoValorDolar.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtNuevoValorDolar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNuevoValorDolarKeyTyped(evt);
            }
        });
        jPanel2.add(txtNuevoValorDolar, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, 80, -1));

        jButton2.setText("Modificar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 80, 80, 30));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, 300, 120));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Respaldo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jPanel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Estado :");
        jPanel3.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 80, -1, -1));

        jTextField9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel3.add(jTextField9, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 40, 260, -1));

        jButton4.setText("Respaldar");
        jButton4.setEnabled(false);
        jPanel3.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 110, 90, 30));

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel10.setText("Ruta :");
        jPanel3.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        jButton7.setText("Seleccionar");
        jButton7.setEnabled(false);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 70, 90, 30));

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 210, 330, 220));

        filler1.setBackground(new java.awt.Color(255, 153, 51));
        filler1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(filler1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 250, 0, 200));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Imprecion", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jPanel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setText("Imprimir ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 53, 80, 30));
        jPanel4.add(txtimp, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 30, 80, -1));

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel11.setText("Nro ° Factura a imprimir :");
        jPanel4.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 150, 20));

        getContentPane().add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 330, 300, 100));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Base De Datos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jPanel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel5.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel13.setText("Estado :");
        jPanel5.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 80, -1, -1));

        jTextField10.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel5.add(jTextField10, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 40, 260, -1));

        jButton6.setText("Seleccionar");
        jButton6.setEnabled(false);
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel5.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 70, 90, 30));

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel14.setText("Ruta :");
        jPanel5.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        getContentPane().add(jPanel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 80, 330, 130));

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Gasoil", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jPanel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jPanel6.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Precio GasOil :");
        jPanel6.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));

        txtValorGasOil.setEditable(false);
        txtValorGasOil.setFont(new java.awt.Font("Tahoma", 0, 12));
        jPanel6.add(txtValorGasOil, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 80, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("Actual");
        jPanel6.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, -1, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("Nuevo");
        jPanel6.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 20, -1, -1));

        txtNuevoValorGasOil.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtNuevoValorGasOil.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNuevoValorGasOilKeyTyped(evt);
            }
        });
        jPanel6.add(txtNuevoValorGasOil, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, 80, -1));

        jButton5.setText("Modificar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel6.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 80, 80, 30));

        getContentPane().add(jPanel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 200, 300, 130));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        JFileChooser fc = null;
        String ruta;
        
        if (fc == null) {
            fc = new JFileChooser();
            
        }
        int estado = fc.showSaveDialog(null);
        File file = fc.getSelectedFile();
        
        if (file != null && estado == JFileChooser.APPROVE_OPTION) {
            ruta = (file.getAbsolutePath());
            String f = ("config.txt");
            File f1 = new File(f);
            
            if (f1.exists()) {
                BufferedWriter bw;
                try {
                    bw = new BufferedWriter(new FileWriter(f));
                    bw.write(ruta);
                    bw.close();
                } catch (IOException ex) {
                    Logger.getLogger(FrmConfiguraciones.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                
            } else {
                try {
                    BufferedWriter bw = new BufferedWriter(new FileWriter(f));
                    bw.write(ruta);
                    bw.close();
                } catch (IOException ex) {
                    Logger.getLogger(FrmConfiguraciones.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }
            System.out.println(ruta);
        } else if (estado == JFileChooser.CANCEL_OPTION) {
        }
    }//GEN-LAST:event_jButton6ActionPerformed
    
    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        try {
            // TODO add your handling code here:

            
            
            
            String source = "SoftTruckBD.mdb";
            Date d = new Date();
            String destiny = "C:\\Users\\Javs\\Desktop\\SoftTruckBD" + d.toString() + ".mdb";
            
            File sourceFile = new File(source);
            File destinyFile = new File(destiny);
            InputStream in = null;
            try {
                in = new FileInputStream(sourceFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FrmConfiguraciones.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            OutputStream out = null;
            try {
                out = new FileOutputStream(destinyFile);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FrmConfiguraciones.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            byte[] buf = new byte[2048];
            int len;
            try {
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } catch (IOException ex) {
                Logger.getLogger(FrmConfiguraciones.class.getName()).log(Level.SEVERE, null, ex);
            }
            in.close();
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(FrmConfiguraciones.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_jButton7ActionPerformed
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if ("".equals(txtNuevoValorDolar.getText())) {
            JOptionPane.showMessageDialog(rootPane, "Ingrese nuevo valor del dolar");
        } else {
            double nuevaCambio = Double.parseDouble(this.txtNuevoValorDolar.getText());
            GestCambio.getInstance().modificar1(1, nuevaCambio, Double.parseDouble(txtValorGasOil.getText()));
            // double 1 = GestCambio.getInstance().devolverUnicoCambio();
            this.txtValorDolar.setText(String.valueOf(GestCambio.getInstance().devolverUnicoCambio().getDolares()));

            //    GestCambio.getInstance().modificarCambio(cam.getDolares());
            this.txtNuevoValorDolar.setText("");
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void txtNuevoValorDolarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNuevoValorDolarKeyTyped
        // TODO add your handling code here:
        /*char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }*/
    }//GEN-LAST:event_txtNuevoValorDolarKeyTyped
    
    private void txtNuevoValorGasOilKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNuevoValorGasOilKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNuevoValorGasOilKeyTyped
    
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        if ("".equals(txtNuevoValorGasOil.getText())) {
            JOptionPane.showMessageDialog(rootPane, "Ingrese nuevo valor del GasOil");
        } else {
            double cambio = Double.parseDouble(txtValorDolar.getText());
            double nuevaGasoil = Double.parseDouble(this.txtNuevoValorGasOil.getText());
            GestCambio.getInstance().modificar1(1, cambio, nuevaGasoil);
            // double 1 = GestCambio.getInstance().devolverUnicoCambio();
            this.txtValorGasOil.setText(String.valueOf(GestCambio.getInstance().devolverUnicoCambio().getGasOil()));
            this.txtNuevoValorGasOil.setText("");
        }
    }//GEN-LAST:event_jButton5ActionPerformed
    
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    try {
        String xx = this.txtimp.getText();
        int i = Integer.parseInt(xx);
        
        con = BaseDeDatos.getConexion();
        JasperDesign jd = JRXmlLoader.load("iReport\\javo.jrxml");
        if (GestFactura.getInstance().exsistefactura(i)==true) {
            String x = "Select * from facturas where fNumero=" + i + "";
            JRDesignQuery consulta = new JRDesignQuery();
            consulta.setText(x);
            jd.setQuery(consulta);
            JasperReport jr = JasperCompileManager.compileReport(jd);
            JasperPrint jp = JasperFillManager.fillReport(jr, null, con);
            JasperViewer.viewReport(jp, false);
        } else {
            JOptionPane.showMessageDialog(rootPane, "No existe esa factura");
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, "No existe el numero de factura");
    }
}//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.Box.Filler filler1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField9;
    private javax.swing.JTextField txtNuevoValorDolar;
    private javax.swing.JTextField txtNuevoValorGasOil;
    private javax.swing.JTextField txtValorDolar;
    private javax.swing.JTextField txtValorGasOil;
    private javax.swing.JTextField txtimp;
    // End of variables declaration//GEN-END:variables
}
